#!/usr/bin/env python

import json
import restclient

class Base(object):
    """Base Model class
    
    Provides a nicer representation when a class instance is printed.
    Found on the SqlAlchemy wiki.
    """
    
    def __repr__(self):
        return "%s:\n    %s" % (
            (self.__class__.__name__),
            '\n    '.join(["%s: %r" % (key, getattr(self, key))
                           for key in sorted(self.__dict__.keys())
                           if not key.startswith('_')]))

    def getJson(self, url):
        """Perform Rest GET to url.
        
        Because https://yande.re use bad ssl certificate.
        We must edit restclient library and change line 90 and 323:
            h = httplib2.Http(disable_ssl_certificate_validation=True)
        
        Args:
            url: A string where the rest resource located.
            
        Returns:
            A tuple of response header and response body.
        """
        
        responses = restclient.GET(url, async=False, resp=True)
        responses[0]['status'] = int(responses[0]['status'])
        data = json.loads(responses[1])
        return (responses[0], data)
